/* Copyright [2023] <gyl> */

#include "sql/executor/utils.h"

#include "sql/optimizer/logical_plan_generator.h"
#include "sql/optimizer/physical_plan_generator.h"
#include "sql/stmt/utils.h"
#include "storage/db/db.h"


RC ExecutorUtils::operate_priv_record(Stmt* stmt, Db* db) {
  Utils utils;
  unique_ptr<LogicalOperator> logical_operator;
  LogicalPlanGenerator logical_generator_;
  RC rc = RC::SUCCESS;
  rc = logical_generator_.create(stmt, logical_operator);
  if (rc != RC::SUCCESS) {
    return rc;
  }
  unique_ptr<PhysicalOperator> physical_operator;
  PhysicalPlanGenerator physical_generator_;
  rc = physical_generator_.create(*logical_operator, physical_operator);
  if (rc != RC::SUCCESS) {
    return rc;
  }
  rc = utils.open(physical_operator, db);
  if (rc != RC::SUCCESS) {
    return rc;
  }
  rc = physical_operator->next();
  if (rc != RC::SUCCESS && rc != RC::RECORD_EOF) {
    return rc;
  }
  rc = utils.close(physical_operator);
  return rc;
}
